﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KnitwearCompany.Managers;
using KnitwearCompany.Models;

namespace KnitwearCompany.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private Manager manager = new Manager();

        public ActionResult Index()
        {
            ViewData["categories"] = manager.GetCategories();
            return View();
        }


        public ActionResult ViewCategory(int id)
        {
            ViewData["products"] = manager.GetProducts(id);
            ViewData["category"] = manager.GetCategory(id);
            return View();
        }

        public ActionResult ViewProduct(int id)
        {
            return View(manager.GetProduct(id));
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Buy(int id, string amount)
        {
            var product = manager.GetProduct(id);
            var purchase = new PurchaseItem();
            purchase.Product = product;
            purchase.Amount = Convert.ToInt32(amount);
            if (Session["Cart"] == null)
                Session["Cart"] = new Cart();
            (Session["Cart"] as Cart).AddPurchase(purchase);
            return View(product);
        }

        public ActionResult ViewCart()
        {
            if (Session["Cart"] == null)
                Session["Cart"] = new Cart();
            return View(Session["Cart"]);
        }

        public ActionResult RemovePurchaseItem(int id)
        {
            (Session["Cart"] as Cart).RemovePurchase( manager.GetProduct(id) );
            return RedirectToAction("ViewCart");
        }

        public ActionResult CreateOrder()
        {
            if (Session["User"] == null)
                return RedirectToAction("Login", "Account");
            var order = new Order();
            order.User = (User)Session["User"];
            order.Purchases = ((Cart) Session["Cart"]).Purchases;
            return View(order);
        }

        [HttpPost]
        public ActionResult CreateOrder([Bind(Exclude = "ID")]Order order)
        {
            try
            {
                order.User = (User)Session["User"];
                order.Date = DateTime.Now;
                order.Status = manager.GetStatuses().First();
                order.Purchases = ((Cart)Session["Cart"]).Purchases;
                order = manager.AddOrder(order);
                if (order.Id != 0)
                {
                    return RedirectToAction("Success", new { order.Id });
                }
                else
                    return RedirectToAction("CreateOrder");
            }
            catch
            {
                return RedirectToAction("CreateOrder");
            }
        }

        public ActionResult Success(int id)
        {
            return View(manager.GetOrder(id));
        }

        public ActionResult ViewOrder(int id)
        {
            return View(manager.GetOrder(id));
        }

    }
}
